package com.weixing.mall.provider.controller.admin;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.annotation.NoNeedAccess;
import com.weixing.mall.core.annotation.SysLog;
import com.weixing.mall.core.enums.OperateEnum;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.core.support.auth.SysAuth;
import com.weixing.mall.core.support.auth.SysInfo;
import com.weixing.mall.provider.model.domain.AfterSaleImg;
import com.weixing.mall.provider.model.dto.*;
import com.weixing.mall.provider.model.query.AfterSaleProblemQuery;
import com.weixing.mall.provider.model.query.AfterSaleQuery;
import com.weixing.mall.provider.model.vo.AfterSaleOrderDetilVo;
import com.weixing.mall.provider.model.vo.AfterSaleProblemVo;
import com.weixing.mall.provider.model.vo.AfterSaleVo;
import com.weixing.mall.provider.service.IAfterSaleImgService;
import com.weixing.mall.provider.service.IAfterSaleService;
import com.weixing.mall.provider.support.excel.AfterSaleOrderExcalDownLoad;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 后台售后/退款/换货API
 *
 * @author gjq
 * @date 2019/10/15
 * @since 1.0
 **/
@RestController
@RequestMapping("afterSale")
public class AfterSaleController extends BaseController {

    @Autowired
    private IAfterSaleService afterSaleService;
    @Autowired
    private IAfterSaleImgService  afterSaleImgService;

    /**
     * 售后订单管理列表
     *
     * @param query  查询参数
     * @return 结果
     */
    @GetMapping("selectAfterSaleList")
    public Result<IPage<AfterSaleVo>> selectAfterSaleList(AfterSaleQuery query) {
        IPage<AfterSaleVo> list = afterSaleService.selectAfterSaleList(initMpPage(), query);
        return ResultUtil.success(list);
    }

    /**
     * 售后订单管理列表EXCAL导出
     *
     * @param query    查询条件
     * @param response HttpServletResponse
     */
    @PostMapping("getAfterSaleListExcal")
    @SysLog(type = OperateEnum.D, desp = "售后订单管理列表导出")
    private void excalDownload(@RequestBody AfterSaleQuery query, HttpServletResponse response) {
        List<AfterSaleVo> list = afterSaleService.selectAfterSaleList(query);
        AfterSaleOrderExcalDownLoad.ExcalDownload(list, response);
    }

    @ApiOperation(value = "售后订单修改售后类型")
    @PostMapping("afterSaleChgType")
    public Result afterSaleChgType(@Validated AfterSaleChgTypeDto afterSaleChgTypeDto) {
        afterSaleService.afterSaleChgType(afterSaleChgTypeDto);
        return ResultUtil.success();
    }

    @ApiOperation(value = "售后订单修改是否问题订单")
    @PostMapping("afterSaleChgProblem")
    public Result afterSaleChgProblem(@SysInfo SysAuth auth, @Validated AfterSaleChgProblemDto afterSaleChgProblemDto) {
        afterSaleService.afterSaleChgProblem(afterSaleChgProblemDto, auth);
        return ResultUtil.success();
    }

    @ApiOperation(value = "售后订单修改售后状态")
    @PostMapping("afterSaleChgStatus")
    public Result afterSaleChgStatus(@Validated AfterSaleChgStatusDto ascsd) {
        if (ascsd.getIds() != null && ascsd.getIds().size() > 0) {
            ascsd.getIds().forEach(id -> {
                synchronized (id) {
                    AfterSaleChgStatusDto a = new AfterSaleChgStatusDto();
                    a.setId(id);
                    a.setStatus(ascsd.getStatus());
                    afterSaleService.afterSaleChgStatus(a);
                }
            });
            return ResultUtil.success();
        } else if (ascsd.getId() != null && ascsd.getStatus() != null) {
            afterSaleService.afterSaleChgStatus(ascsd);
            return ResultUtil.success();
        }
        return ResultUtil.error("失败");
    }

    @ApiOperation(value = "（售后管理）售后订单详情换货")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "售后订单id", paramType = "path", dataType = "long")
    })
    @GetMapping("/getAfterSaleOrderDetil/{id}")
    public Result getAfterSaleOrderDetil(@PathVariable Long id) {
        AfterSaleOrderDetilVo afterSaleOrderDetilVo = afterSaleService.selectAfterSaleOrderDetil(id);
        return ResultUtil.success(afterSaleOrderDetilVo);
    }

    @ApiOperation(value = "（售后管理）售后订单换货确认操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "售后订单id", paramType = "path", dataType = "long"),
            @ApiImplicitParam(name = "orderId", value = "订单orderId", paramType = "path", dataType = "long")
    })
    @GetMapping("afterConfirm/{id}/{orderId}")
    @NoNeedAccess
    public Result getAfterSaleOrderDetil(@PathVariable Long id, @PathVariable Long orderId) {
        afterSaleService.confirmHandleAfterOrder(id, orderId);
        return ResultUtil.success();
    }

    @ApiOperation(value = "售后客服备注")
    @GetMapping("afterSaleChgCustomerRemark")
    public Result afterSaleChgCustomerRemark(ChgCustomerRemarkDto chgCustomerRemarkDto) {
        afterSaleService.afterSaleChgCustomerRemark(chgCustomerRemarkDto);
        return ResultUtil.success();
    }

    @ApiOperation(value = "问题订单列表")
    @GetMapping("selectAfterSaleProblemList")
    public Result selectAfterSaleProblemList(AfterSaleProblemQuery afterSaleProblemQuery) {
        IPage<AfterSaleProblemVo> list = afterSaleService.selectAfterSaleProblemList(initMpPage(), afterSaleProblemQuery);
        return ResultUtil.success(list);
    }

    @ApiOperation(value = "变更售后问题订单是否处理")
    @PostMapping("afterSaleChgIsHandle")
    public Result afterSaleChgIsHandle(@RequestBody @Validated AfterSaleChgIsHandleDto afterSaleChgIsHandleDto) {
        if (ObjectUtil.isEmpty(afterSaleChgIsHandleDto) || afterSaleChgIsHandleDto.getId() == null) {
            return ResultUtil.error("服务参数可不用");
        }
        afterSaleService.afterSaleChgIsHandle(afterSaleChgIsHandleDto);
        return ResultUtil.success();
    }

    @ApiOperation(value = "二期更改退换货状态")
    @PostMapping("changeStatus")
    @NoNeedAccess
    public Result changeStatus(@RequestBody AfterSaleChgTypeDto afterSaleChgTypeDto) {
        String msg = afterSaleService.changeStatus(afterSaleChgTypeDto);
        if ("1".equals(msg)) {
            return ResultUtil.error("退换货切换一致，不进行修改");
        }
        if ("2".equals(msg)) {
            return ResultUtil.error("售后状态不正确，不进行修改");
        }
        return ResultUtil.success();
    }

    /**
     *   根据售后ID查询搜索图片
     * @param afterOrderId  售后订单ID
     * @return
     */
    @ApiOperation(value = "二期查看订单瑕疵图片")
    @GetMapping("afterImgList/{afterOrderId}")
    public Result afterImgList(@PathVariable Long  afterOrderId) {
        List<AfterSaleImg>  afterSaleImgList=afterSaleImgService.getListByAfterId(afterOrderId);
        return ResultUtil.success(afterSaleImgList);
    }
}
